<?php

namespace app\merchant\model;

use think\Model;

class Order extends Model
{
    protected $type = [
        'address_info'    =>  'json',
    ];

    public static function dataList($where,$page,$num,$field="*",$order="id desc"){
        return self::where($where)->field($field)->order($order)->page($page,$num)->select();
    }

    public static function dataCount($where){
        return self::where($where)->count();
    }

    /***订单信息
     * @param $where array
     * @return json
     */
    public static function dataView($where){
        return self::where($where)->find();
    }


    /*** 新增订单
     * @param $data
     * @return $id
     */
    public static function dataAdd($data){
        return self::create($data);
    }


    /*** 修改订单
     * @param $data
     * @return bool
     */
    public static function dataEdit($where,$data){
        return self::where($where)->update($data);
    }


    public static function dataAddAll($data){
        return self::json(["address_info"])->insertAll($data);
    }

    /***  批量统计
     * @param $field
     * @param $where
     * @param $group
     * @return mixed
     */
    public static function dataCountList($field,$where,$group){
        return self::field($field)->where($where)->group($group)->select();
    }

    public static function dataCountNum($where,$whereRaw = "1=1"){
        return self::where($where)
            ->whereRaw($whereRaw)
            ->count();
    }

    /***
     * 链表查询订单详细信息
     */
    public static function dataListJoinAll($where,$field="o.*,u.phone,u.nickname",$sort = "o.id desc"){
        $res = self::alias("o")
            ->leftJoin("user u","o.uid = u.uid")
            ->where($where)
            ->order($sort)
            ->field($field)
            ->select()
            ->toArray();
        return $res;
    }
}